import java.util.*;

public class Solution18 {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Set<List<Integer>> ans=new HashSet<>();
        Set<Integer> set=new HashSet<>();
        Arrays.sort(nums);
        set.add(nums[0]);
        for (int i=1;i< nums.length;i++){
            for (int j=i+1;j<nums.length;j++){
                for (int k=j+1;k<nums.length;k++){
                    if (set.contains(target-nums[i]-nums[j]-nums[k])){
                        List<Integer> list=new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[k]);
                        list.add(target-nums[i]-nums[j]-nums[k]);
                        list.sort(Comparator.comparingInt(a -> a));
                        ans.add(list);
                    }
                }
            }
            set.add(nums[i]);
        }
        return new ArrayList<>(ans);
    }

    public static void main(String[] args) {
        System.out.println(new Solution18().fourSum(new int[]{1,0,-1,0,-2,2},0));
    }
}
